#coding=utf-8
from django.shortcuts import render
from django.http import HttpResponse
from models import Users,Lessons,Teacherlesson

from django.conf import settings
import os
# from models import UserHeader
def myinfo(request):
	if 'id' in request.session:
		u=Users.objects.filter(id=int(request.session["id"]))
		context={"user":u[0]}
		return render(request,"myinfo.html",context)
	return render(request,"login.html")

# 我的资料页面
def mydata(request):
	if 'id' in request.session:
		u=Users.objects.filter(id=int(request.session["id"]))
		context={"user":u[0]}
		return render(request,"mydata.html",context)
	return render(request,"login.html")

# 修改个人资料
def modifyData(request):
	if 'id' in request.session:
		user=Users.objects.filter(id=request.session['id'])[0]
		# 判断是否添加文件
		if 'img' in request.FILES:
			# 判断之前是否有图片
			# path='%s/media/%s' % (settings.BASE_DIR,user.src)
			# path=path.replace("\\","/")
			# os.remove(path)
			user.src=request.FILES['img']
		user.nickname=request.POST['nickname']
		user.age=request.POST['age']
		user.hobby=request.POST['hobby']
		user.eduBackgroud=request.POST['eduBackgroud']
		user.school=request.POST['school']
		user.grade=request.POST['grade']
		user.reward=request.POST['reward']
		user.englishLevel=request.POST['englishLevel']
		user.address=request.POST['address']
		user.save()
		return HttpResponse("<script>alert('修改完成！');window.history.back(-1);</script>")
	return render(request,"login.html")
	pass

# 退出登录
def logout(request):
	request.session.flush()
	return render(request,"login.html")

# 我的课程
def getlessonlist(request):
	if 'id' in request.session:
		user=Users.objects.filter(id=request.session['id'])[0]
		context={}
		context['user']=user
		if user.identity == 0:
			# 该用户为学生
			lessonlist=Lessons.objects.filter(users=request.session['id'])
			context["lessonlist"]=lessonlist
			context["identity"]=0
		else:
			lessons=Teacherlesson.objects.filter(users=request.session['id'])	
			context["lessonlist"]=lessons
			context["identity"]=1
		return render(request,"mylesson.html",context)
	return render(request,"login.html")

def finishOrder(request):
	lessonid = request.POST['lessonid']
	lesson=Lessons.objects.filter(id=lessonid)[0]
	lesson.status=2
	lesson.save()
	tealesson=Teacherlesson.objects.filter(lessons_id=lessonid)[0]
	tealesson.status=1
	tealesson.save()
	return HttpResponse("OK")
	pass